import Mesh from "./mesh";
import ColorMaterial from './colorMaterial';
import BoxMeshInfo from '../primitive/boxmeshInfo';

class Entity{
    constructor(meshInfo) {

        this._worldMat = cc.mat4();
        this._mvpMat = cc.mat4();

        this._colorMat = new ColorMaterial();
        this._mesh = new Mesh(meshInfo);

        // OBB包围盒的数据
        this._debugDraw = false;
        this._debugOBB = null;
    }

    setCullBack(value){
        this._mesh._bCullBack = value;
    }

    setWireFrame(value){
        this._mesh._bWireFrame = value;
    }

    setDebugObb(value){
        this._debugDraw = value;
        if(this._debugDraw){
            if(!this._debugOBB){
                this._debugOBB = new DebugOBB(this._mesh._meshInfo);
                this._debugOBB._colorMat.setColor(cc.Color.RED);
            }
        }
    }

    applyWorldMatrix(worldMat){
        cc.Mat4.copy(this._worldMat,worldMat);
    }

    /**
     * 调用此方法后, 所有的顶点都变换到了世界坐标系, 存储在this._finalVertexs[i]里面.
     * 实际用于绘制的 索引 全部存储在 this._finalIndices 中
     * @param camera
     */
    cullFace(camera){
        this._mesh.cullFace(camera,this._worldMat);
        if(this._debugDraw){
            this._debugOBB.applyWorldMatrix(this._worldMat);
            this._debugOBB.cullFace(camera);
        }
    }

    render(camera){
        this._mesh.render(camera,this._colorMat);
        if(this._debugDraw){
            this._debugOBB.render(camera);
        }
    }
}

class DebugOBB extends Entity{

    /**
     *
     * @param min [x,y,z]
     * @param max [x,y,z]
     */
    constructor(meshInfo){
        let boxMeshInfo = new BoxMeshInfo(meshInfo);
        super(boxMeshInfo);
    }
}

export default Entity;
